<?php

define('ROOT_PATH', str_replace('\\', '/', __DIR__) . '/');


spl_autoload_register(function($class){

	$prefix = "Foo\\";

	$base_dir = ROOT_PATH . '/src/';

	$len = strlen($prefix);
	if (strncmp($prefix, $class, $len) !== 0) {
		return ;
	}

	$rele_class = substr($class, $len);

	$file = $base_dir . str_replace('\\', '/', $rele_class) . '.php';

	if (file_exists($file)) {
		require $file;
	}


});

spl_autoload_register(function($class){

	$prefix = "Scripts\\";

	$base_dir = ROOT_PATH . '/scripts/';

	$len = strlen($prefix);
	if (strncmp($prefix, $class, $len) !== 0) {
		return ;
	}

	$rele_class = substr($class, $len);

	$file = $base_dir . str_replace('\\', '/', $rele_class) . '.php';

	if (file_exists($file)) {
		require $file;
	}


});

/**
 * 获取配置 db.user.test
 */
function C($path) {

	$config_dir = ROOT_PATH . '/config/';
	$config = explode('.', trim($path, '.'));
	$len = count($config);

	if ($len === 1) {
		if (file_exists($config_dir . $config[0] . '.php')) {
			return require $config_dir . $config[0] . '.php';
		}
		return array();
	}

	$c = require $config_dir . array_shift($config) . '.php';

	$eval_str = '$c';
	foreach ($config as $k) {
		$eval_str .= "['{$k}']";
	}
	@eval('$c = ' . $eval_str . ';');

	return $c;
}